



// 修改积分规则
function openUpdateProtect() {
    var integralType = $("#integralType").val();
    var isList = $("#isList").val();
    var key = $("#key").val();
    var params;
    if (isList=='true') {
        params = toArray($("#" + key).serializeArray());
    }else {
        params = $("#" + key).serializeJSON();
    }
    debugger
    if (key=="RATIO") {
        let money=params.money
        var regular = /^[1-9]\d*\.[1-9]$|0\.[1-9]$|^([^0]\d*)$|^0$/;
        var isTrue= regular.test(money);
        if (!isTrue) {
            parent.layer.msg("只能保留一位小数")
            return;
        }
        if (params.isMoney == undefined && params.isGoods == undefined) {
            parent.layer.msg("兑换方式至少选择一项");
            return;
        }
        if (params.isGoods == undefined) {
            params.isGoods = 'off';
        }
        if (params.isMoney == undefined) {
            params.isMoney = 'off';
        }

    }
    var data={
        key:key,
        content:params
    };
    $.ajax({
        url:baseloaction+"/admin/user/cus/updateIntegralRuleConfig",
        data:JSON.stringify(data),
        dataType: "json",
        type:"post",
        contentType:"application/json",
        success:function (res) {

            if (res.success==true) {
                parent.layer.msg("修改成功");
            }else{
                parent.layer.msg(res.msg);
            }
            if (integralType=='config') {
                parent.getRedirectMenu("/admin/user/cus/config/toIntegralRule", false);
            }else{
                parent.getRedirectMenu("/admin/user/cus/rule/toIntegralRule", false);
            }

            parent.layer.closeAll()
        }
    })
}

function toObject(unit,score,max){
    var temp = new Object();
    temp.unit = unit;
    temp.score = score;
    if (max != null) {
        temp.max=max;
    }
    return temp;
}
function toArray(tempArray) {
    var integralRuleList = new Array();
    if (tempArray.length==6) {
        for (let i = 0; i < tempArray.length; i=i+2) {
            integralRuleList.push(toObject(tempArray[i].value,tempArray[i+1].value));
        }
    }else{
        for (let i = 0; i < tempArray.length; i=i+3) {
            integralRuleList.push(toObject(tempArray[i].value,tempArray[i+1].value,tempArray[i+2].value));
        }
    }


    return integralRuleList;
}
